/*
* @Author: xfy
* @Date:   2017-09-07 10:25:00
* @Last Modified by:   xfy
* @Last Modified time: 2017-09-12 18:53:57
*/
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _supper         = require('util/supper.js');
var _attendance     = require('service/attendance-service.js');
var Pagination      = require('util/pagination/index.js');
var templateIndex   = require('./index.string');

//page逻辑部分
var page = {
    data : {
        listParam : {
            pageNum     : 1,
            pageSize    : 5,
            idNo        : _supper.getUrlParam('idNo')
        }
    },
    init : function(){
        this.bindEvent();
        this.onLoad();
    },
    onLoad : function(){
        //加载商品信息
        this.loadProductList();
    },
    bindEvent : function(){
        var _this           = this;
        //跳转页面
        $(document).on('click', '.attendance-attendance-input', function(){
            window.location.href = './attendance-create.html?idNo=' + _supper.getUrlParam('idNo');
        });

        //点击修改考勤状态
        $(document).on('click', '.order-list-table .cell-stop', function(){
            var attendanceInfo = {
                idNo            : $('#idNo').text(),
                createTime      : $(this).parent().prev().text(),
                status          : 0,
            };
            
            //更新考勤状态
            _attendance.updateAttendance(attendanceInfo, function(res, msg){
                _supper.successTips("修改考勤状态成功");
                _this.loadProductList();
                //window.setTimeout(function() {window.location.href = './attendance.html?idNo=' + $('#idNo').text();}, 1500);
            }, function(errMsg){
                _supper.errorTips("修改考勤状态失败");
            });
            
        });

        //点击修改考勤状态
        $(document).on('click', '.order-list-table .cell-start', function(){
            var attendanceInfo = {
                idNo            : $('#idNo').text(),
                createTime      : $(this).parent().prev().text(),
                status          : 1,
            };
            
            //更新考勤状态
            _attendance.updateAttendance(attendanceInfo, function(res, msg){
                _supper.successTips("修改考勤状态成功");
                //window.setTimeout(function() {window.location.href = './attendance.html?idNo=' + $('#idNo').text();}, 1500);
                _this.loadProductList();
            }, function(errMsg){
                _supper.errorTips("修改考勤状态失败");
            });
            
        });
    },
    //验证搜索id时，是否为数字
    validateId : function(data){
        return !$.isNumeric(data);
    },
    //加载所有记录
    loadProductList : function(){
        var _this           = this,
            orderListHtml   = '',
            $listCon        = $('.attendance-page-wrap .attendance-con');
        $listCon.html('<div class="loading"></div>');
        _attendance.getAttendance(this.data.listParam, function(res){
            //渲染html
            orderListHtml = _supper.renderHtml(templateIndex, res);
            $listCon.html(orderListHtml);
            _this.loadPagination({
                hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,
                pageNum         : res.pageNum,
                pages           : res.pages
            });
        }, function(errMsg){
            //window.location.href = './user-login.html?redirect=./attendance.html';
            $listCon.html('<div class="error-tip"><p class="error-msg"><i class="fa fa-frown-o"></i>' + errMsg + '</p></div>');
        });

    },
    //加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container : $('.pagination'),
            onSelectPage : function(pageNum){
                _this.data.listParam.pageNum = pageNum;
                _this.loadProductList();
            }
        }));
    }
};
$(function(){
    page.init();
});